package com.ailiwu.www.ailiwu.Fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.BaseFragment;

import java.util.ArrayList;

/**
 * Author:    zzw
 * Version    V2.0
 * Date:      2015/12/3
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/2 0002       XiYoung              1.0                   1.0
 * 2015/12/3            zzw                  2.0                   1.0
 * Why & What is modified:
 */
public class ClassifyFragment extends BaseFragment{
    private TabLayout mTabClassify;
    private ViewPager mVpClassify;
    private Adapter mAdapter;
    protected ArrayList<Fragment> fragments = new ArrayList<>();
    protected ArrayList<String> tabList = new ArrayList<>();
    private final static String STR1 = "攻略专题";
    private final static String STR2 = "礼物大全";
    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragment_classify,null);
    }

    @Override
    protected void initView(View view) {
        /**设置ViewPager和Tab的适配*/
        setTabViewPager(view);
    }


    @Override
    protected void initData() {
        fragments.add(new ClassifyStrategyFragmnet());
        fragments.add(new ClassifyGiftFragmnet());

        tabList.add(STR1);
        tabList.add(STR2);

        mTabClassify.setupWithViewPager(mVpClassify);
        mAdapter.notifyDataSetChanged();
    }

    private void setTabViewPager(View view) {
        mVpClassify = (ViewPager) view.findViewById(R.id.vp_classify);
        mTabClassify = (TabLayout) view.findViewById(R.id.tab_classify);
        mAdapter = new Adapter(getActivity().getSupportFragmentManager());
        mVpClassify.setAdapter(mAdapter);
    }
    /**
     * Tab与Viewpager适配器
     */
    class Adapter extends FragmentPagerAdapter {

        public Adapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabList.get(position);
        }
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (getView() != null) {
            getView().setVisibility(menuVisible ? View.VISIBLE : View.INVISIBLE);
        }
    }

}
